import seaborn as sns
import pandas as pd
import matplotlib.pyplot as plt
import matplotlib as mpl
from io import StringIO

mpl.rcParams['pdf.fonttype'] = 42
mpl.rcParams['ps.fonttype'] = 42
mpl.rcParams['axes.linewidth'] = 1.5
mpl.rcParams['lines.linewidth'] = 3
mpl.rcParams['lines.markersize'] = 10
mpl.rcParams['font.size'] = 15
mpl.rcParams['xtick.labelsize'] = 14
mpl.rcParams['ytick.labelsize'] = 16
mpl.rcParams['legend.fontsize'] = 14
mpl.rcParams['legend.framealpha'] = 0
mpl.rcParams['legend.borderpad'] = 0.1
mpl.rcParams["axes.labelweight"] = "bold"
mpl.rcParams["axes.labelsize"] = 14
mpl.rcParams['font.family'] = 'sans-serif'
mpl.rcParams['figure.figsize'] = (9, 4)
# mpl.rc("font",family='MicroSoft YaHei',weight="bold")

csv = '''Network,System,Size,Time
4323:4323,Baseline,26
4323:4323,Path switching,16
4323:4323,Fusion-fixed,8,2732.932
4323:4323,Fusion-var,8,51.281
3549:3549,Baseline,25
3549:3549,Path switching,17
3549:3549,Fusion-fixed,9,16309.277
3549:3549,Fusion-var,8,87.554
Abilene,Baseline,9
Abilene,Path switching,7
Abilene,Fusion-fixed,3,6.002
Abilene,Fusion-var,3,2.891
ATMnet,Baseline,12
ATMnet,Path switching,11
ATMnet,Fusion-fixed,4,12.233
ATMnet,Fusion-var,3,5.539
BBNPlanet,Baseline,14
BBNPlanet,Path switching,10
BBNPlanet,Fusion-fixed,7,22.456
BBNPlanet,Fusion-var,5,8.303
BICS,Baseline,17
BICS,Path switching,13
BICS,Fusion-fixed,8,39.333
BICS,Fusion-var,7,8.9
Chinanet,Baseline,9
Chinanet,Path switching,7
Chinanet,Fusion-fixed,7,49.018
Chinanet,Fusion-var,6,11.582
'''

df = pd.read_csv(StringIO(csv), sep=",")
print(df)

def plot_size():
  sns.barplot(x = 'Network',
              y = 'Size',
              hue = 'System',
              data = df)
  plt.ylabel('Max label size')
  plt.xlabel('Network')
  plt.legend().set_title(None)
  # plt.yscale('log')
  plt.tight_layout(pad=0.1)
  plt.savefig('./figures/dpg/eval-size.pdf')
  # Show the plot
  plt.show()
  
def plot_time():
  d = df.loc[df['Time'].isna() == False]
  sns.barplot(x = 'Network',
              y = 'Time',
              hue = 'System',
              data = d)
  plt.ylabel('Time (s)')
  plt.xlabel('Network')
  plt.legend().set_title(None)
  plt.yscale('log')
  plt.tight_layout(pad=0.1)
  plt.savefig('./figures/dpg/eval-time.pdf')
  # Show the plot
  plt.show()


def stat():
  baselines = df.loc[df['System'] == 'Baseline']['Size'].tolist()
  fusionfixed = df.loc[df['System'] == 'Fusion-fixed']['Size'].tolist()
  fusionvar = df.loc[df['System'] == 'Fusion-var']['Size'].tolist()
  fusionps = df.loc[df['System'] == 'Path switching']['Size'].tolist()
  
  ratio = [(i-j)/i for i,j in zip(baselines, fusionfixed)]
  ratio = [(i-j)/i for i,j in zip(baselines, fusionvar)]
  # ratio = [(i-j)/i for i,j in zip(fusionps, fusionfixed)]
  ratio = [(i-j)/i for i,j in zip(fusionps, fusionvar)]
  print(ratio)

stat()
# plot_size()
# plot_time()

  